package com.founder.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.founder.model.User;
import com.founder.service.IUserService;
import com.founder.util.CommonUtil;
import com.github.pagehelper.Page;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private IUserService userService;

	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(@RequestParam(value = "name") String name,
			@RequestParam(value = "password") String password,
			HttpSession session,HttpServletRequest request) 	throws Exception {
		// shiro在认证过程中出现错误后将异常类路径通过request返回
		Subject user = SecurityUtils.getSubject();  
		  
        UsernamePasswordToken token = new UsernamePasswordToken(name, password);
		String exceptionClassName = (String) request
				.getAttribute("shiroLoginFailure");
	    try {  
            user.login(token);  
            String userID = (String) user.getPrincipal();  
          
  
            session.setAttribute("USERNAME", userID);  
            return "redirect:/main.html";  
        } catch (UnknownAccountException uae) {  
         
        } catch (IncorrectCredentialsException ice) {  
          
        } catch (LockedAccountException lae) {  
          
        } catch (AuthenticationException e) {  
           
            e.printStackTrace();  
            token.clear();  
        }  
	
		return "login";

	}

	@RequestMapping(params = "method=logout")
	@ResponseBody
	public Map<String, Object> logout(HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		session.invalidate();
		map.put("result", true);
		return map;
	}

	@RequestMapping(params = "method=pageUser")
	@ResponseBody
	public Map<String, Object> pageUser(HttpServletRequest request) {
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");

		int pageNum = Integer.parseInt(page);
		int pageSize = Integer.parseInt(rows);
		String keyWord = request.getParameter("keyWord");
		Map<String, Object> map = new HashMap<String, Object>();
		Page<User> pageUser = userService.pageUser(pageNum, pageSize, keyWord);
		map.put("rows", pageUser.getResult());
		map.put("total", pageUser.getTotal());
		return map;
	}

	
	@RequestMapping( value="/new",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> addUser(@ModelAttribute("user") User user) {
		user.setId(CommonUtil.uuid());
		boolean result = this.userService.addUser(user);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", result);
		if (result) {
			map.put("user", user);
		} else {
			map.put("tip", "娣诲姞澶辫触銆�");
		}
		return map;
	}

	@RequestMapping(params = "method=modUser")
	@ResponseBody
	public Map<String, Object> modUser(@ModelAttribute("user") User user) {
		boolean result = this.userService.modUser(user);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", result);
		if (result) {
			map.put("user", user);
		} else {
			map.put("tip", "淇敼澶辫触銆�");
		}
		return map;
	}

	@RequestMapping(params = "method=delUserById")
	@ResponseBody
	public Map<String, Object> delUserById(HttpServletRequest request) {
		String id = request.getParameter("id");
		boolean result = this.userService.delUserById(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", result);
		if (!result) {
			map.put("tip", "鍒犻櫎澶辫触銆�");
		}
		return map;
	}

}
